#include <bits/stdc++.h>
#define ru(i,l,r) for(int i=(l);i<=(r);i++)
#define ru_(i,l,r) for(int i=(l);i<(r);i++)
#define rd(i,l,r) for(int i=(l);i>=(r);i--)
#define ll long long
#define pii pair<int,int>

using namespace std;


// 位运算， 计数枚举， 图遍历||图遍历
class Solution {
public:
    int smallestNumber(int n) {
        return (1<<(__lg(n)+1))-1;
    }
};

class Solution2 {
public:
    int getLargestOutlier(vector<int>& nums) {
        multiset<int> ms; ll sm=0;
        for(int x: nums) ms.insert(x), sm+=x;
        ll ans=-1e9;
        for(int x: nums){
            ms.erase(ms.find(x));
            ll t=sm-x*2;
            if(ms.find(t)!=ms.end()) ans=max(ans,t);
            ms.insert(x);
        }; return ans;
    }
};

class Solution3 {
public:
    vector<int> maxTargetNodes(vector<vector<int>>& edges1, vector<vector<int>>& edges2, int k) {
        int n=edges1.size()+1,N=edges2.size()+1;
        vector<int> e[n],E[N];
        for(auto &x: edges1){
            e[x[0]].push_back(x[1]); e[x[1]].push_back(x[0]);
        }
        for(auto &x: edges2){
            E[x[0]].push_back(x[1]); E[x[1]].push_back(x[0]);
        }
        auto dfs=[&](auto &&self,int sn,int fa,int rem,vector<int> *e)->int{
            int ret=1;
            if(rem>0)
                for(int fn:e[sn])
                    if(fn!=fa)
                        ret+=self(self,fn,sn,rem-1,e);
            return ret;
        };
        vector<int> ans(n);
        ru_(i,0,n) ans[i]=dfs(dfs,i,-1,k,e);
        if(k>0){
            int best=0;
            ru_(i,0,N) best=max(best,dfs(dfs,i,-1,k-1,E));
            ru_(i,0,n) ans[i]+=best;
        }
        return ans;
    }
};

class Solution4 {
public:
    vector<int> maxTargetNodes(vector<vector<int>>& edges1, vector<vector<int>>& edges2) {
        int n=edges1.size()+1,N=edges2.size()+1;
        vector<int> e[n],E[N];
        for(auto &x: edges1){
            e[x[0]].push_back(x[1]); e[x[1]].push_back(x[0]);
        }
        for(auto &x: edges2){
            E[x[0]].push_back(x[1]); E[x[1]].push_back(x[0]);
        }
        int a[n],c[2]={0};
        int A[N],C[2]={0};
        auto dfs=[&](auto &&self,int sn,int fa,int col,int *a,int *c,vector<int> *e)->void{
            a[sn]=col; c[col]++;
            for(int fn:e[sn])
                if(fn!=fa) self(self,fn,sn,col^1,a,c,e);
        };
        dfs(dfs,0,-1,0,a,c,e);
        dfs(dfs,0,-1,0,A,C,E);
        vector<int> ans(n);
        ru_(i,0,n) ans[i]=c[a[i]];
        if(N==1) return ans;
        int mx=max(C[0],C[1]);
        for(int i=0;i<n;i++) ans[i]+=mx;
        return ans;
    }
};
// 12mins

int main(){
    Solution s; cout<<s.smallestNumber(5)<<endl;
    vector<int> nums={2,3,5,10};
    Solution2 s2; cout<<s2.getLargestOutlier(nums)<<endl;

    vector<vector<int>> edges1={{0,1},{0,2},{2,3},{2,4}};
    vector<vector<int>> edges2={{0,1},{0,2},{0,3},{2,7},{1,4},{4,5},{4,6}};
    Solution3 s3; vector<int> ans3=s3.maxTargetNodes(edges1, edges2, 2); for(auto a: ans3) cout<<a<<" "; cout<<endl;

    Solution4 s4; vector<int> ans4=s4.maxTargetNodes(edges1, edges2); for(auto a: ans4) cout<<a<<" "; cout<<endl;
}